<?php

/**
 * Albireo Kernel
 *
 * @copyright  Copyright (c) 2010 Albireo Solutions
 * @package    Kernel
 */

namespace KernelModule\LogsModule\AclModule;

use Nette\Object;
use Nette\Environment;
use Nette\Debug;
use \dibi;
use \DibiDataSource;

/**
 * Kernel ACL logs base class.
 *
 * @author Pavel
 */
class AclLogs extends Object {
    
    private $dataSource = NULL;

    /**
     * Retrieves the data source, some kind of queryset in the django language
     * @return DibiDataSource
     */
    public function getDataSource($date = "") {
        if ($this->dataSource === NULL)
            $this->dataSource = new DibiDataSource("acl_log", dibi::getConnection());
        $this->dataSource->orderBy("time", dibi::DESC);
        return $this->dataSource;
    }

    /**
     * Counts the number of the logs in DB, which were created after the given
     * timestamp parameter, i.e they are newer.
     *
     * @todo WTF??? Check, why if we substract 1 second from the timestamp, we
     * get totaly different result (even if there is no midnight log).
     *
     * @param string $timestamp the timestamp form of the datetime we want to count older than
     * @return int the count of the ACL logs in DB newer than (created after or
     * right in the time of) the $timestamp parameter
     */
    public function countNewerThan($timestamp) {
        $date = date("H:i:s, j.m. Y", $timestamp);
        //throw new \Exception($date); //what a strange behaviour here
        $dataSource = new DibiDataSource("acl_log", dibi::getConnection());
        $dataSource->where("[time] >= %d", $timestamp);
        return $dataSource->count();
    }

}